package com.hx.myfristandriod;

import java.io.IOException;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class TestXmlActivity extends Activity{

	private TextView xmlTxtView;
	private Button xmlBtn;
	@Override
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.test_xml);
		xmlTxtView=(TextView)findViewById(R.id.TxtView11);
		xmlBtn=(Button)findViewById(R.id.button11);
		xmlBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				  int counter=0;
				  StringBuilder sb=new StringBuilder("");
				  //获取xml文件
				  Resources r=getResources();
				  XmlResourceParser xrp=r.getXml(R.xml.test);
				    try{	
				    	int xrpType=xrp.getEventType();
				    	//如果文件没有结尾继续循环
				    	while(xrpType!=XmlPullParser.END_DOCUMENT){
				    		//如是开始标签
				    		 if(xrpType==XmlPullParser.START_DOCUMENT){
				    			 String name=xrp.getName();
				    			   if(name.equals("customer")){
				    				   counter++;
				    				   sb.append("第"+counter+"条客户信息："+"\n");
				    				   sb.append(xrp.getAttributeValue(0)+"\n");
				    				   sb.append(xrp.getAttributeValue(1)+"\n");
				    				   sb.append(xrp.getAttributeValue(2)+"\n");
				    				   sb.append(xrp.getAttributeValue(3)+"\n");
				    			   }
				    		 }else if(xrp.getEventType()==XmlPullParser.END_TAG){
				    			 
				    		 }else if(xrp.getEventType()==XmlPullParser.TEXT){
				    			 
				    		 }
				    		//下一个
				    		 System.out.println("这是啥么子："+ xrp.nextText());
				    		 //xrp.nextText();
				    		 
				    	}//while
				    	xmlTxtView.setText(sb.toString());
				    }catch(XmlPullParserException e){
				    	e.printStackTrace();
				    }catch(IOException e){
				    	e.printStackTrace();
				    }		
			}
		});		//click	
	}
}
